/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.ipleiria.plp;

import de.enough.polish.ui.ChoiceGroup;
import de.enough.polish.ui.List;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Filipe
 */
public abstract class Utils {

    public static String[] getSelectedStrings(ChoiceGroup cg) {

        int[] selected = getSelectedIndexes(cg);

        if (selected.length == 0) {
            return null;
        }

        String[] returnStrings = new String[selected.length];

        for (int i = 0; i < selected.length; i++) {
            returnStrings[i] = cg.getString(selected[i]);
        }

        return returnStrings;
    }

    public static String[] getSelectedStrings(List ls) {

        int[] selected = getSelectedIndexes(ls);

        if (selected.length == 0) {
            return null;
        }

        String[] returnStrings = new String[selected.length];

        for (int i = 0; i < selected.length; i++) {

            returnStrings[i] = ls.getString(selected[i]);
        }

        return returnStrings;
    }

    public static int[] getSelectedIndexes(ChoiceGroup cg) {

        boolean[] returnedBols = new boolean[cg.size()];
        cg.getSelectedFlags(returnedBols);

        return processBols(returnedBols, cg.size());
    }

    public static int[] getSelectedIndexes(List ls) {

        boolean[] returnedBols = new boolean[ls.size()];
        ls.getSelectedFlags(returnedBols);
        for (int i = 0; i < ls.size(); i++) {
            System.out.println("list inside getSelectedInd " + i + ": " + returnedBols[i]);
        }
        return processBols(returnedBols, ls.size());
    }

    private static int[] processBols(boolean[] bols, int size) {

        int s = 0;

        for (int i = 0; i < size; i++) {
            if (bols[i]) {
                s++;
            }
        }
        int ret[] = new int[s];
        int g = 0;
        for (int i = 0; i < size; i++) {
            if (bols[i]) {
                System.out.println("True is index: "+i);
                ret[g] = i;
                g++;
            }
        }

        return ret;


    }

    /**
     * Util to split strings as the midp doesnt support it natively,
     * adapted from source:
     * http://discussion.forum.nokia.com/forum/showpost.php?p=269060&postcount=4
     * @param original the original string to be split
     * @return
     */
    public static String[] split(String original, String separator) {
        Vector nodes = new Vector();

        // Parse nodes into vector
        int index = original.indexOf(separator);
        while (index >= 0) {
            nodes.addElement(original.substring(0, index));
            original = original.substring(index + separator.length());
            index = original.indexOf(separator);
        }
        // Get the last node
        nodes.addElement(original);

        // Create splitted string array
        String[] result = new String[nodes.size()];
        if (nodes.size() > 0) {
            for (int loop = 0; loop < nodes.size(); loop++) {
                result[loop] = (String) nodes.elementAt(loop);
                System.out.println(result[loop]);
            }

        }

        return result;
    }

    public static byte[] imageToBytes(Image im) {

        int w = im.getWidth();
        int h = im.getHeight();
        int[] argb = new int[w * h];
        im.getRGB(argb, 0, w, 0, 0, w, h);

        System.out.println("Bytes: " + argb.toString());
        ByteArrayOutputStream baos = new ByteArrayOutputStream(w * h);
        DataOutputStream dos = new DataOutputStream(baos);

        for (int i = 0; i < argb.length; i++) {
            try {
                dos.writeInt(argb[i]);
            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }
        }
        byte[] imaData = baos.toByteArray();

        return imaData;
    }
}
